package com.hong.collections.blocking;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.ArrayBlockingQueue;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;


/**
 * @Auther: Administrator
 * @Date: 2018/8/10 17:32
 * @Description:
 */
public class ArrayBlockingQueueExampleTest {

    private ArrayBlockingQueueExample example;

    @Before
    public void setUp(){
        example = new ArrayBlockingQueueExample();
    }

    @After
    public void tearDown(){
        example = null;
    }

    /**
     * 没有超过容量的时候会立即加入
     */
    @Test
    public void testAddMethodNotExceedCapacity(){
        ArrayBlockingQueue<String> queue = example.create(5);
        assertThat(queue.add("Hello1"),equalTo(true));
        assertThat(queue.add("Hello2"),equalTo(true));
        assertThat(queue.add("Hello3"),equalTo(true));
        assertThat(queue.add("Hello4"),equalTo(true));
        assertThat(queue.add("Hello5"),equalTo(true));
        assertThat(queue.size(),equalTo(5));
    }

    @Test(expected = IllegalStateException.class)
    public void testAddMethodExceedCapacity(){
        ArrayBlockingQueue<String> queue = example.create(5);
        assertThat(queue.add("Hello1"),equalTo(true));
        assertThat(queue.add("Hello2"),equalTo(true));
        assertThat(queue.add("Hello3"),equalTo(true));
        assertThat(queue.add("Hello4"),equalTo(true));
        assertThat(queue.add("Hello5"),equalTo(true));
        assertThat(queue.add("Hello6"),equalTo(true));
        fail("should not process to here.");
    }


}
